import 'package:enterprise_v6_app/app/components/common/custom_no_data.dart';
import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_plan_entity.dart';

/// 续保记录
class RenewalRecord extends StatefulWidget {
  const RenewalRecord({super.key});

  @override
  State<RenewalRecord> createState() => _RenewalRecordState();
}

class _RenewalRecordState extends State<RenewalRecord> {
  /// 事故事件数据
  EmergencyEventListEntity item = Get.arguments as EmergencyEventListEntity;

  // 续保记录数据
  List<EmergencyEventPlanEntity> renewalRecordData = [];

  // 获取续保记录数据
  void getRenewalRecordData() async {
    final res = await HttpUtil().generateGetRequest<List<EmergencyEventPlanEntity>>(
      Interface.getAccidentInfoReport,
      queryParameters: {
        'id': item.id,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        renewalRecordData = result.data!;
        if (mounted) {
          setState(() {});
        }
      }
    });
  }

  @override
  void initState() {
    super.initState();
    getRenewalRecordData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Container(
            height: AppDimens.dp12,
            color: AppColors.bg_color_grey,
          ),
          Container(
            height: AppDimens.dp2,
            color: AppColors.blue_button,
          ),
          Expanded(
              child: Container(
            alignment: Alignment.topCenter,
            color: const Color(0xffffffff),
            child: renewalRecordData.isEmpty
                ? const CustomNoData()
                : ListView.builder(
                    padding: EdgeInsets.symmetric(horizontal: 27.w, vertical: 22.w),
                    itemCount: renewalRecordData.length,
                    shrinkWrap: true,
                    physics: const BouncingScrollPhysics(),
                    itemBuilder: (context, index) {
                      return CustomIntrinsicCircleLine(
                        showWidget: _getView(renewalRecordData[index], index),
                      );
                    }),
          ))
        ],
      ),
    );
  }

  ///获取事故显示
  Widget _getView(EmergencyEventPlanEntity item, int index) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Text(
              '第${index + 1}次续报',
              style: TextStyle(
                fontSize: 15.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xff262F38),
              ),
            ),
            AppGaps.hGap12,
            Text(
              item.updateDate,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
                color: const Color(0xff495061),
              ),
            ),
          ],
        ),
        AppGaps.vGap12,
        Container(
          padding: EdgeInsets.all(12.w),
          alignment: Alignment.topLeft,
          decoration: BoxDecoration(
            color: const Color(0xffF7F7FB),
            border: Border.all(
              color: const Color(0xffEBEDF0),
              width: 1.w,
            ),
            borderRadius: BorderRadius.circular(4.r),
          ),
          constraints: BoxConstraints(minHeight: 80.w),
          child: SingleChildScrollView(
            child: Column(
              children: [
                CustomTextWidget.getTextRowView(title: '事故最新情况：', value: item.accidentUpdate),
                AppGaps.vGap14,
                CustomTextWidget.getTextRowView(title: '处置状态：', value: item.handleStateLabel),
                AppGaps.vGap14,
                CustomTextWidget.getTextRowView(title: '事故现场图片：', value: ''),
                AppGaps.vGap12,
                Align(
                  alignment: Alignment.centerLeft,
                  child: Wrap(
                    spacing: 10.w,
                    runSpacing: 10.w,
                    children: item.accidentScenePhotos.map(_buildPicItem).toList(),
                  ),
                )
              ],
            ),
          ),
        ),
        AppGaps.vGap16,
      ],
    );
  }

  Widget _buildPicItem(String url) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: SizedBox(
        height: 77.w,
        width: 77.w,
        child: Image.network(
          url.startsWith('http') ? url : Interface.fileUrl + url,
          fit: BoxFit.cover,
        ),
      ),
    ).clickShowLarge(
      url: url.startsWith('http') ? url : Interface.fileUrl + url,
    );
  }
}
